/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


 ************************************************************************************************
 Objet type FORM
 VERSION:0.85
 DATE:16/03/2006
 ************************************************************************************************
 Init parameter :
 Name:               Form name
 url:                url to call
 parent              form parent
 objPar              object of parameter
 		method              form method
 		idCont              context id
 		autoSubmit:         Validate when "Enter" is typed
		boolWithFenWait		if set to true will display a wait popup during the load
		boolAsynch			using asynchronous call (ajax,iFrame..)
 		imgBtonSubmit       reference of an object(image or button ...) that will submit the form after click
		callBackObjectOnSuccess: object who have the onsucces callback method 
		canBeCancel:if set to true, the request can be abort
		onSuccess:onsucces callback method  
		boolIframe:if true use iframe instead of xmlHTTPRequest

 ************************************************************************************************/

// *************************************
// CONSTRUCTOR
// *************************************
rialto.widget.Form=function(formName,url,parent,objPar){
// PROPRIETES        
        //base object parameter
	    var objParam={
	    		type:"form",
	    		name:formName
	    	}
        //base constructor
	    this.base = rialto.widget.AbstractContainer;    
	    this.base(objParam);
	    
	    //Default value
        this.action=url;
        this.url = url;
        this.autoSubmit=true;
        this.imgBtonSubmit = null;
        this.idCont = null;
        this.boolWithFenWait=true;   
        this.boolAsynch=true;   
	  	this.method="get";
	  	this.boolIframe=false;
	  	this.canBeCancel=true;
	  	
	    //set the value with the properties of the objPar object
	    if (objPar){               
	        if (objPar.imgBtonSubmit){
	            this.imgBtonSubmit = objPar.imgBtonSubmit;        
	        }        
	        if (rialto.lang.isBoolean(objPar.autoSubmit)){
	            this.autoSubmit = objPar.autoSubmit;        
	        }
	        if (rialto.lang.isStringIn(objPar.method,["post","get"])){
	            this.method = objPar.method;        
	        }
	        if (rialto.lang.isBoolean(objPar.boolWithFenWait)){
	            this.boolWithFenWait = objPar.boolWithFenWait;
	        }
	        if (rialto.lang.isBoolean(objPar.boolAsynch)){
	            this.boolAsynch = objPar.boolAsynch;
	        }  
	        if (objPar.idCont){
	            this.idCont = objPar.idCont;
	            var primCar=this.url.indexOf("?")!=-1 ? '&' : '?';
	            this.url +=primCar+'FENID='+objPar.idCont;			    
	        }
	        if (rialto.lang.isBoolean(objPar.canBeCancel)){
           		this.canBeCancel = objPar.canBeCancel;        
	        }
	        if (rialto.lang.isBoolean(objPar.boolIframe)){
           		this.boolIframe = objPar.boolIframe;        
	        }
	        if (objPar.onSuccess){
	            this.onSuccess =objPar.onSuccess;		
	        }                 
	        if (objPar.callBackObjectOnSuccess){
            	this.callBackObjectOnSuccess = objPar.callBackObjectOnSuccess;        
       	 	}
	    }  
		// *******************************************
		// GENERATION OF DOM ELEMENTS
		// *******************************************

    	var oThis=this;
	    this.formulaire = document.createElement("FORM");     
   		this.formulaire.style.position="relative";
		this.formulaire.method=this.method;
		
		//remote ajaxRequest
		if(!this.boolIframe){
			this.remote=new rialto.io.AjaxRequest({
						url:this.url,
						method: this.method, 
						callBackObjectOnSuccess:this.callBackObjectOnSuccess,
						withWaitWindow:this.boolWithFenWait,
						canBeCancel:this.canBeCancel,
						onSuccess: this.onSuccess
					})
		}
	    // submit when "Enter" is typed
	    if (this.autoSubmit){
	        this.formulaire.onkeypress = function (evt) {
	        	if(!evt){evt=window.event;}
	        	var key = evt.keyCode ? evt.keyCode :
                            evt.charCode ? evt.charCode :
                                            evt.which ? evt.which : void 0;                        
	            if (key==13){
	                // d?sactive la s?lection
	                document.onselectstart=new Function ("return false")                
	                oThis.submitForm();                
	                return false;
	            }
	        }
	    }   

    	//submit onclick of the imgBtonSubmit
	    if (this.imgBtonSubmit)  {
	        this.imgBtonSubmit.oCiu = this;
	        this.affComportBtonSubmit();
	    }
		//place in the parent
    	if (parent) {this.placeIn(parent);};   
		//release the objects
		objPar=null;
		objParam=null;
	
}

rialto.widget.Form.prototype = new rialto.widget.AbstractContainer;
rialto.widget.Form.prototype.tabIndex=1;
rialto.widget.Form.prototype.nbReq = 0;
//===========================


rialto.widget.Form.prototype.release = function (imgBtonSubmit) {
    this.formulaire.onkeypress=null;
    if (this.imgBtonSubmit)  {
    	if(this.imgBtonSubmit.remove){this.imgBtonSubmit.remove()};
    	this.imgBtonSubmit.oCiu=null;
    	this.imgBtonSubmit.onclick=null;
    }
}


rialto.widget.Form.prototype.addBtonSubmit = function (imgBtonSubmit) {
    this.imgBtonSubmit = imgBtonSubmit;
    this.imgBtonSubmit.oCiu = this;
    this.affComportBtonSubmit();
}

rialto.widget.Form.prototype.affComportBtonSubmit = function () {
        this.imgBtonSubmit.onclick = function () {
                this.oCiu.submitForm();
        }
}

rialto.widget.Form.prototype.getHtmlExt = function () {
	// overloading rialto.widget.AbstractComponent.getHtmlExt
    return this.formulaire;
}

rialto.widget.Form.prototype.getHtmlCont = function () {
     // overloading rialto.widget.AbstractContainer.
	return this.formulaire;
}


rialto.widget.Form.prototype.buildQueryString=function() {
    //This fonction will build the querry string by parsing the elements of the form
    var qs = ''
    for (e=0;e<this.formulaire.elements.length;e++) {
        //get the element
        var elmt=this.formulaire.elements[e];
        if (elmt.name!='') {
            //For the radio add only the value of the one checked
            if (elmt.type=="radio" && elmt.checked==true ){
                qs+='&'+elmt.name+'='+escape(elmt.value)
            }
            else{
            	//For the other add whatever
	            if (elmt.type!="radio"){
    	            qs+='&'+elmt.name+'='+escape(elmt.value)
        	    }
        	}
        }
    }
	//if querry string remove the first & 
    if (qs!=''){qs=qs.substring(1);}   
    return qs;
}

rialto.widget.Form.prototype.resetForm=function () {
	this.formulaire.reset();
}

rialto.widget.Form.prototype.submitForm=function () {  
//================================     
    //check if can be submit
    if( this.onSubmitForm() ){ 
	    if (this.boolAsynch){
		    // build the querry string and call the server
	        if(!this.boolIframe){
	        	this.remote.load(this.buildQueryString());
	        }
	        else{       
    	    //WTH IFRAME....DEPRECATED
	    	    var idExtReq = ++rialto.widget.Form.prototype.nbReq;
		        if (!this.iframe) {
		            this.iframe=new objFrame("ifr",0,0,0,0);          
		            this.iframe.create(this.formulaire);
		        }
		        var primCar=this.url.indexOf("?")!=-1 ? '&' : '?';
		        this.lastURL=this.url+primCar+this.buildQueryString();
		        alert(this.lastURL);
				this.iframe.load(this.lastURL);
				//lanct ?ventuel du sablier avec possibilit? d'interruption de la requete        
		        if (this.boolWithFenWait){
					//this.fenWait=new objFenWait("TRAITEMENT EN COURS",this.url, this.idCont)        
		            this.fenWait=new objFenWait("TRAITEMENT EN COURS",this.lastURL, this.idCont, idExtReq)        
		        }
		    }
		}
		else{
			this.formulaire.action=this.action;
			this.formulaire.submit();
		}		
        //specific treatement after submit
        this.afterSubmitForm();     
        
    } 
}


//The code must be redefine after the instanciation of the component. By default this method does nothing.
//this method return a boolean that indicate if submit must be perform or not
rialto.widget.Form.prototype.onSubmitForm=function () { return true;}

//The code must be redefine after the instanciation of the component. By default this method does nothing.
//to do after submit
rialto.widget.Form.prototype.afterSubmitForm=function () {return true;}


//To change the url to call
rialto.widget.Form.prototype.setURL=function(url){
    this.url =url; 
	//if there is a context id it will be add to the URL    
    if(this.idCont){
	    var primCar=this.url.indexOf("?")!=-1 ? '&' : '?';
	    this.url +=primCar+'FENID='+this.idCont;
	}                           
}
